/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var picture_config = {
    'folder' : 'images/picture/',
    'namafile' : ['002.png','003.png','004.png','005.png','006.png','007.png','008.png','009.png'],
    'notfoundPic' : 'gagal.png',
    'startIDX' : 4,
    'icon_folder' : 'images/icon'
};

var default_avatar = new Array();
default_avatar[0] = 'images/avatar1.jpg';
default_avatar[1] = 'images/avatar2.jpg';
default_avatar[2] = 'images/avatar3.gif';
/*var default_comment ={
    '0' : ['keren gila','mantabs gan'],
    1 : ['sampah','ra mutu'],
    2 : ['pertamax gan'],
    3 : ['opo iki???'],
    4 : ['REPOST!!!!'],
    5 : ['Numpang lewat kk...'],
    6 : ['senangnya hatiku turun panas demamku'],
    7 : ['kenapa ini kk']
};*/
var default_comment = new Array(picture_config.namafile.length);
for(var i=0;i<default_comment.length;i++){
    default_comment[i] = new Array();
}
default_comment[0][0] = 'keren gila';
default_comment[0][1] = 'mantabs gan';
default_comment[1][0] = 'sampah';
default_comment[1][1] = 'ra mutu';
default_comment[2][0] = 'opo iki??';
default_comment[3][0] = 'REPOST!!!';
default_comment[4][0] = 'Numpang Lewat kk...';
default_comment[5][0] = 'lalalallala';
default_comment[6][0] = 'pertamax gan';
default_comment[7][0] = 'kenapa ini kk';




function generatePicture(config,comment){
    if(!config){
        config = picture_config;
    }
    if(!comment){
        comment = default_comment;
    }
    this.currentID=config.startIDX;

    //generate fungsi-fungsinya
    this.updatePic = f_updatePic;
    this.showComment = f_showComment;
    this.addComment = f_addComment;

    //register to global function
    //array of picture ID
    if(!window.pictureObject)
        window.pictureObject = this;
    //cek div
    this.picDiv = document.getElementById('bigpic_wrapper');
    if(!this.picDiv){
        this.picDiv = document.createElement("div");
        this.picDiv.id = 'bigpic_wrapper';
        document.body.appendChild(this.picDiv);
    }
    this.leftDiv = document.getElementById('prevPicture');
    if(!this.leftDiv){
        this.leftDiv = document.createElement("div");
        this.leftDiv.id='prevPicture';
        this.picDiV.appendChild(this.leftDiv);
    }
    this.mainDiv = document.getElementById('mainPicture');
    if(!this.leftDiv){
        this.leftDiv = document.createElement("div");
        this.leftDiv.id='mainPicture';
        this.picDiV.appendChild(this.leftDiv);
    }
    this.rightDiv = document.getElementById('nextPicture');
    if(!this.leftDiv){
        this.leftDiv = document.createElement("div");
        this.leftDiv.id='nextPicture';
        this.picDiV.appendChild(this.leftDiv);
    }
    this.picture = document.getElementById('picture');
    if(!this.picture){
        this.picture = document.createElement("div");
        this.picture.id='picture';
        this.mainDiv.appendChild(this.picture);
    }
    this.inputComment = document.getElementById('comm');
    //addeventlistener    
    this.config = config;
    this.comment = comment;
    if(!this.updatePic(this.currentID))
        return;

}
function f_updatePic(prevORNext){
    //cek kebenaran current ID
    if(prevORNext)
        this.currentID-=1;
    else
        this.currentID+=1;    
    if(this.currentID > this.config.namafile.length - 1)
        this.currentID = 0;
    if(this.currentID < 0)
        this.currentID = this.config.namafile.length - 1;
    //alert(this.currentID);
    //this.mainDiv.style.backgroundImage = 'url(' + this.config.folder + this.config.namafile[this.currentID] +')';
    //alert(this.config.folder + this.config.namafile[this.currentID]);
    this.picture.src = this.config.folder + this.config.namafile[this.currentID];
    
    this.showComment(this.currentID);
}

function f_showComment(currentID){
    var obj = document.getElementById('comment');
    var s='';
    for(var i=0;i<default_comment[currentID].length;i++){
        var rand = Math.floor(Math.random() * 3);
        s+='<div class="comment_place"><img src="'+default_avatar[rand]+'"/><div>' + default_comment[currentID][i] +'</div></div>';
    }
    obj.innerHTML = s;    
    document.getElementById('comm').focus();
    document.getElementById('comm').value='';
    window.scroll(0,0);
    //alert(window.scrollMaxY);
    //setTimeout(pageScroll(0, window.scrollMaxY), 500);
}
function pageScroll(x,y){
    window.scroll(x, y);
}
function f_addComment(comment,currentID){
    if(!comment || comment==''){
        return;
    }
    if(!currentID){
        currentID = this.currentID;
    }
    this.comment[currentID][this.comment[currentID].length] = comment;
    var length = this.comment[currentID][this.comment[currentID].length-1].length;
    var count = this.comment[currentID][this.comment[currentID].length-1].length /50;
    var sisa = this.comment[currentID][this.comment[currentID].length-1].length % 50;
    for(var i=0;i<count;i++){
        this.comment[currentID][this.comment[currentID].length-1]= this.comment[currentID][this.comment[currentID].length-1].substr(0, (i+1)*50)
            +"<br/>"
            +this.comment[currentID][this.comment[currentID].length-1].substr(((i+1)*50)+1, length);
    }
    this.comment[currentID][this.comment[currentID].length-1]= this.comment[currentID][this.comment[currentID].length-1].replace("\n","<br/>","g");
    f_showComment(currentID);
}
function start(){

}

